// Copyright (C) 2010 Razvan Constantin <razvan.constantin@rconst.net>
//
// This file is part of FileBow.
//
// FileBow is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// FileBow is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with FileBow.  If not, see <http://www.gnu.org/licenses/>.

#include "common/strings.h"

#include <algorithm>
#include <vector>

#include "common/common.h"

using namespace std;

void Split(const string& s, char delim, vector<string>* parts) {
  CHECK_PTR(parts);
  for (istringstream iss(s); !iss.eof(); ) {
    string part;
    getline(iss, part, delim);
    parts->push_back(part);
  }
}

vector<string> Split(const string& s, char delim) {
  vector<string> v;
  Split(s, delim, &v);
  return v;
}

string Join(const vector<string>& parts, const string& sep) {
  string s;
  for (int i = 0; i < parts.size(); ++i) {
    s.append(i ? sep : "");
    s.append(parts[i]);
  }
  return s;
}

bool StartsWith(const string& s, const string& prefix) {
  if (prefix.empty() || prefix.length() > s.length()) {
    return false;
  }
  return mismatch(s.begin(), s.end(), prefix.begin()).second == prefix.end();
}
